package com.bbx.taxi.client.DB.MyOrder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrderList;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrderDetail;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.VehicleDetail;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.VehicleInfo;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.End;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Location;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Start;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Handler;

/**
 * 我的订单
 * 
 */
public class MyOrderingDB implements DBcolumns {
	public static boolean isRead=true;
	private DBHelper helper;

	public MyOrderingDB(Context context) {
		MyApplication myApplication=MyApplication.getInstance();
		helper = DBHelper.getInstance(context,myApplication.getUid());
	}

	/**
	 * 打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() {
		SQLiteDatabase SQLdb = helper.getReadableDatabase();
		return SQLdb;
	}

	/**
	 * 打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() {
		SQLiteDatabase SQLdb = helper.getWritableDatabase();
		return SQLdb;
	}

	/**
	 * 查询全部
	 */
	public Cursor onQuery() {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_MYORDERING, null,
				MYORDERING_ORDER_STATUS+"="+Value.ORDER_STATUS_KF_GAIPAI+" or "+
				MYORDERING_ORDER_STATUS+"="+Value.ORDER_STATUS_GAIPAI+" or "+
				MYORDERING_ORDER_STATUS+"="+Value.ORDER_STATUS_OFFCAR+" or "+MYORDERING_ORDER_STATUS+" <= "+Value.ORDER_STATUS_OFFCAR, null, null,
				null, MYORDERING_id + " desc");
		return cursor;

	}
	/**
	 * 根据订单id查询
	 */
	public Cursor onQuery(String order_id) {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_MYORDERING, null, MYORDERING_ORDER_ID
				+ "='" + order_id + "'", null, null, null, MYORDERING_id
				+ " desc ");
		return cursor;

	}
	/**
	 * 用于分页查询，指定数量，指定id,按降序查询，不能大于id,MYORDERING_ORDER_STATUS，因为0.1.2,3,4则进行中的订单,其他为完成订单
	 */
	public Cursor onQuery(int id, int count) {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = null;
		if (id == -1) {
			cursor = SQLdb.query(TABLE_MYORDERING, null,
					MYORDERING_ORDER_STATUS+"="+Value.ORDER_STATUS_KF_GAIPAI+" or "+
					MYORDERING_ORDER_STATUS+"="+Value.ORDER_STATUS_GAIPAI+" or "+
					MYORDERING_ORDER_STATUS+"="+Value.ORDER_STATUS_OFFCAR+" or "+MYORDERING_ORDER_STATUS+" <= "+Value.ORDER_STATUS_OFFCAR, null, null,
					null, MYORDERING_id + " desc  LIMIT 0," + count);
		} else {
			cursor = SQLdb.query(TABLE_MYORDERING, null, MYORDERING_id + "<"
					+ id+" and ("+MYORDERING_ORDER_STATUS+"="+Value.ORDER_STATUS_GAIPAI+" or "+
					MYORDERING_ORDER_STATUS+"="+Value.ORDER_STATUS_OFFCAR+" or "+MYORDERING_ORDER_STATUS+" <= "+Value.ORDER_STATUS_OFFCAR+" )", null, null, null, MYORDERING_id + " desc  LIMIT "
					+ count);
		}

		return cursor;

	}

	/**
	 * 删除整个表
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb = onOpenWrite();
		SQLdb.delete(TABLE_MYORDERING, null, null);
		SQLdb.close();
	}

	/**
	 * 删除
	 */
	public void onDelete(Context context,String order_id) {
		SQLiteDatabase SQLdb = onOpenWrite();
		int i = SQLdb.delete(TABLE_MYORDERING, MYORDERING_ORDER_ID + "='"
				+ order_id + "'", null);
		SQLdb.close();
		//主要进行中的订单不能删除Locations表
	}

	/**
	 * 更新一条数据,根据ID
	 */
	public void onUpdate(String id, int myorder_id, String myorder_order_id,
			int myorder_order_type, String myorder_order_time,
			String myorder_line_id, int myorder_travel_type,
			String myorder_coupon_id, int myorder_order_status,String myorder_valuate_status,
			String driver_id,String driver_name,String car_No,String driver_phone,double driver_star,String order_message,int price,int totalprice,int actualprice) {
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(MYORDERING_ID, myorder_id);
		cv.put(MYORDERING_ORDER_ID, myorder_order_id);
		cv.put(MYORDERING_ORDER_TYPE, myorder_order_type);
		cv.put(MYORDERING_ORDER_TIME, myorder_order_time);
		cv.put(MYORDERING_LINE_ID, myorder_line_id);
		cv.put(MYORDERING_TRAVEL_TYPE, myorder_travel_type);
		cv.put(MYORDERING_COUPON_ID, myorder_coupon_id);
		cv.put(MYORDERING_ORDER_STATUS, myorder_order_status);
		cv.put(MYORDERING_VALUATE_STATUS, myorder_valuate_status);
		
		cv.put(MYORDERING_DRIVER_ID,driver_id);
		cv.put(MYORDERING_DRIVER_NAME,driver_name);
		cv.put(MYORDERING_CAR_NO, car_No);
		cv.put(MYORDERING_DRIVER_PHONE, driver_phone);
		cv.put(MYORDERING_DRIVER_STAR, driver_star);
		cv.put(MYORDERING_DRIVER_MESSAGE, order_message);
		cv.put(MYORDERING_PRICE, price);
		cv.put(MYORDERING_TOTALPRICE, totalprice);
		cv.put(MYORDERING_ACTUALPRICE, actualprice);
		SQLdb.update(TABLE_MYORDERING, cv, MYORDERING_id + "=" + id, null);
		SQLdb.close();
	}
	/**
	 * 更新一条数据,根据order_id修改订单状态
	 */
	public void onUpdate(String myorder_id,int myorder_order_status) {
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(MYORDERING_ORDER_STATUS, myorder_order_status);
		SQLdb.update(TABLE_MYORDERING, cv, MYORDERING_ORDER_ID + "='" + myorder_id+"'", null);
		SQLdb.close();
	}
	/**
	 * 更新一条数据,根据order_id修改审核状态
	 */
	public void onUpdate(String myorder_id,String audit_status) {
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(MYORDERING_AUDIT_STATUS, audit_status);
		SQLdb.update(TABLE_MYORDERING, cv, MYORDERING_ORDER_ID + "='" + myorder_id+"'", null);
		SQLdb.close();
	}
	/**
	 * 更新一条数据,根据order_id修改往返
	 */
	public void onUpdateRound_trip(String myorder_id,String round_trip) {
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(MYORDERING_ROUND_TRIP, round_trip);
		SQLdb.update(TABLE_MYORDERING, cv, MYORDERING_ORDER_ID + "='" + myorder_id+"'", null);
		SQLdb.close();
	}
	/**
	 * 更新一条数据,根据order_id修改司机状态
	 */
	public void onUpdate(String myorder_id,int order_status,String driver_id,String driver_name,String car_NO,String driver_phone,double driver_star) {
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(MYORDERING_ORDER_STATUS, order_status);
		cv.put(MYORDERING_DRIVER_ID, driver_id);
		cv.put(MYORDERING_DRIVER_NAME, driver_name);
		cv.put(MYORDERING_CAR_NO, car_NO);
		cv.put(MYORDERING_DRIVER_PHONE, driver_phone);
		cv.put(MYORDERING_DRIVER_STAR, driver_star);
		SQLdb.update(TABLE_MYORDERING, cv, MYORDERING_ORDER_ID + "='" + myorder_id+"'", null);
		SQLdb.close();
	}
	/**
	 * 更新一条数据,根据order_id修改paidan_vtype
	 */
	public void onUpdateVtype(String myorder_id,String paidan_vtype) {
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(MYORDERING_PAIDAN_VTYPE, paidan_vtype);
		SQLdb.update(TABLE_MYORDERING, cv, MYORDERING_ORDER_ID + "='" + myorder_id+"'", null);
		SQLdb.close();
	}
	/**
	 * 插入一条数据
	 */
	public void onWrite(String myorder_id, String myorder_order_id,
			String myorder_order_type, String myorder_order_time,String myorder_xiadan_time,
			String myorder_line_id, String myorder_travel_type,
			String myorder_coupon_id, String myorder_order_status,String myorder_audit_status,String myorder_valuate_status,
			String driver_id,String driver_name,String car_No,String driver_phone,double driver_star,String order_message,String price,
			String totalprice,String actualprice,String coupon_sum,String tc_id,String vehicle_type_id,String round_trip,
			String purpose,String reject_reason,int vtype,int paidan_vtype,String own_expense,String pay_status,String appoint_type) {
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues values = new ContentValues();
		values.put(MYORDERING_ID, myorder_id);
		values.put(MYORDERING_ORDER_ID, myorder_order_id);
		values.put(MYORDERING_ORDER_TYPE, myorder_order_type);
		values.put(MYORDERING_ORDER_TIME, myorder_order_time);
		values.put(MYORDERING_XIADAN_TIME, myorder_xiadan_time);
		values.put(MYORDERING_LINE_ID, myorder_line_id);
		values.put(MYORDERING_TRAVEL_TYPE, myorder_travel_type);
		values.put(MYORDERING_COUPON_ID, myorder_coupon_id);
		values.put(MYORDERING_VALUATE_STATUS, myorder_valuate_status);
		values.put(MYORDERING_ORDER_STATUS, myorder_order_status);
		values.put(MYORDERING_AUDIT_STATUS, myorder_audit_status);

		values.put(MYORDERING_DRIVER_ID,driver_id);
		values.put(MYORDERING_DRIVER_NAME,driver_name);
		values.put(MYORDERING_CAR_NO, car_No);
		values.put(MYORDERING_DRIVER_PHONE, driver_phone);
		values.put(MYORDERING_DRIVER_STAR, driver_star);
		values.put(MYORDERING_DRIVER_MESSAGE, order_message);
		values.put(MYORDERING_PRICE, price);
		values.put(MYORDERING_TOTALPRICE, totalprice);
		values.put(MYORDERING_ACTUALPRICE, actualprice);
		values.put(MYORDERING_COUPONSUM, coupon_sum);
		values.put(MYORDERING_TC_ID, tc_id);
		values.put(MYORDERING_VEHICLE_ID, vehicle_type_id);
		values.put(MYORDERING_ROUND_TRIP, round_trip);
		
		values.put(MYORDERING_PURPOSE, purpose);
		values.put(MYORDERING_REJECT_REASON, reject_reason);
		values.put(MYORDERING_VTYPE, vtype);
		values.put(MYORDERING_PAIDAN_VTYPE, paidan_vtype);
		values.put(MYORDERING_OWN, own_expense);
		values.put(MYORDERING_PAY_STATUS, pay_status);
		values.put(MYORDERING_APPOINT_TYPE, appoint_type);
		SQLdb.insert(TABLE_MYORDERING, MYORDERING_id, values);
		SQLdb.close();
	}

	/**
	 * 关闭数据库
	 */
	public void onColse() {
		SQLiteDatabase SQLdb = onOpenWrite();
		SQLdb.close();
	}

	/**
	 * 读取进行中的订单，并返回数据,notOffCar=true，则未包含已下车的订单
	 */
	public synchronized ArrayList<OfficialOrderList> onReadDB_ing(final Context context, final int num,final boolean notOffCar) {
		try{
		MyOrderLocationsDB db_locations = new MyOrderLocationsDB(context);
		ArrayList<OfficialOrderList> list_ing = new ArrayList<OfficialOrderList>();
		Cursor c = onQuery(-1, num);
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				OfficialOrderList mOfficialOrderList = new OfficialOrderList();
				mOfficialOrderList.setId(map.get(DBcolumns.MYORDERING_ID));
				mOfficialOrderList
						.setOrder_id(map.get(DBcolumns.MYORDERING_ORDER_ID));
				mOfficialOrderList.setOrder_type(map
						.get(DBcolumns.MYORDERING_ORDER_TYPE));
				//该订单是预约时间，不是下单时间
				mOfficialOrderList.setAppoint_time(map
						.get(DBcolumns.MYORDERING_ORDER_TIME));
				//下单时间
				mOfficialOrderList.setOrder_time(map
						.get(DBcolumns.MYORDERING_XIADAN_TIME));
				mOfficialOrderList.setLine_id(map.get(DBcolumns.MYORDERING_LINE_ID));
				mOfficialOrderList.setTravel_type(map
						.get(DBcolumns.MYORDERING_TRAVEL_TYPE));
				mOfficialOrderList.setCoupon_id(map
						.get(DBcolumns.MYORDERING_COUPON_ID));
				
				mOfficialOrderList.setOrder_status(map
						.get(DBcolumns.MYORDERING_ORDER_STATUS));	
				mOfficialOrderList.setAudit_status(map
						.get(DBcolumns.MYORDERING_AUDIT_STATUS));
				mOfficialOrderList.setValuate_status(map
						.get(DBcolumns.MYORDERING_VALUATE_STATUS));
				mOfficialOrderList.setDriver_id(map
						.get(DBcolumns.MYORDERING_DRIVER_ID));
				mOfficialOrderList.setDriver_name(map
						.get(DBcolumns.MYORDERING_DRIVER_NAME));
				mOfficialOrderList.setCar_NO(map
						.get(DBcolumns.MYORDERING_CAR_NO));
				mOfficialOrderList.setDriver_phone(map
						.get(DBcolumns.MYORDERING_DRIVER_PHONE));
				mOfficialOrderList.setDriver_star(Double.parseDouble(map
						.get(DBcolumns.MYORDERING_DRIVER_STAR)));	
				mOfficialOrderList.setMessage(map
						.get(DBcolumns.MYORDERING_DRIVER_MESSAGE));	
				mOfficialOrderList.setPrice(map
						.get(DBcolumns.MYORDERING_PRICE));
				mOfficialOrderList.setTotal_price(map
						.get(DBcolumns.MYORDERING_TOTALPRICE));
				mOfficialOrderList.setActual_price(map
						.get(DBcolumns.MYORDERING_ACTUALPRICE));
				mOfficialOrderList.setCoupon_sum(map
						.get(DBcolumns.MYORDERING_COUPONSUM));
				mOfficialOrderList.setTcId(map
						.get(DBcolumns.MYORDERING_TC_ID));
				mOfficialOrderList.setVehicle_id(map
						.get(DBcolumns.MYORDERING_VEHICLE_ID));
				mOfficialOrderList.setRound_trip(map
						.get(DBcolumns.MYORDERING_ROUND_TRIP));
				mOfficialOrderList.setOwn_expense(map
						.get(DBcolumns.MYORDERING_OWN));
				mOfficialOrderList.setAppoint_type(map
						.get(DBcolumns.MYORDERING_APPOINT_TYPE));
				String pay_status=map
						.get(DBcolumns.MYORDERING_PAY_STATUS);
				mOfficialOrderList.setPay_status(pay_status!=null?pay_status:Value.PAY_STATUS_NO);
				VehicleDetail mVehicleDetail=new VehicleDetail();
				VehicleInfo mVehicleInfo=new VehicleInfo();
				try{
				mVehicleDetail.setPurpose(map.get(DBcolumns.MYORDERING_PURPOSE));
				mVehicleDetail.setReject_reason(map.get(DBcolumns.MYORDERING_REJECT_REASON));
				mVehicleDetail.setVtype(Integer.parseInt(map.get(DBcolumns.MYORDERING_VTYPE)));
				mVehicleInfo.setVtype(Integer.parseInt(map.get(DBcolumns.MYORDERING_PAIDAN_VTYPE)));
				mVehicleDetail.setVehicleInfo(mVehicleInfo);
				}catch(Exception e){}
				mOfficialOrderList.setVehicleDetail(mVehicleDetail);
				list_ing.add(mOfficialOrderList);
			} while (c.moveToNext());
		}else{
			return  new ArrayList<OfficialOrderList>();
		}
		c.close();
		onColse();
		// 读取Locations表
		for (int j = 0; j < list_ing.size(); j++) {
			OfficialOrderDetail mOfficialOrderDetail = new OfficialOrderDetail();
			Cursor c2 = db_locations.onQuery(list_ing.get(j).getOrder_id());
			c2.moveToFirst();
			if (c2.getCount() > 0) {
				do {
					Map<String, String> map = new HashMap<String, String>();
					for (int i = 0; i < c2.getColumnCount(); i++) {
						map.put(c2.getColumnName(i), c2.getString(i));
					}
					
					Start start = new Start();
					start.setAddress(map
							.get(DBcolumns.MYORDER_LOCATIONS_START_ADDRESS));
					start.setName(map
							.get(DBcolumns.MYORDER_LOCATIONS_START_NAME));
					start.setPhone(map
							.get(DBcolumns.MYORDER_LOCATIONS_START_PHONE));
					Location mLocation_start = new Location();
					mLocation_start.setLat(Double.parseDouble(map
							.get(DBcolumns.MYORDER_LOCATIONS_START_LAT)));
					mLocation_start.setLng(Double.parseDouble(map
							.get(DBcolumns.MYORDER_LOCATIONS_START_LNG)));
					start.setLocation(mLocation_start);				
					End end = new End();
					end.setAddress(map
							.get(DBcolumns.MYORDER_LOCATIONS_END_ADDRESS));
					end.setName(map.get(DBcolumns.MYORDER_LOCATIONS_END_NAME));
					end.setPhone(map.get(DBcolumns.MYORDER_LOCATIONS_END_PHONE));
					Location mLocation_end = new Location();
					mLocation_end.setLat(Double.parseDouble(map
							.get(DBcolumns.MYORDER_LOCATIONS_END_LAT)));
					mLocation_end.setLng(Double.parseDouble(map
							.get(DBcolumns.MYORDER_LOCATIONS_END_LNG)));	
					end.setLocation(mLocation_end);
					mOfficialOrderDetail.setStart(start);
					mOfficialOrderDetail.setEnd(end);
				} while (c2.moveToNext());
			}
			list_ing.get(j).setOrderDetail(mOfficialOrderDetail);
			c2.close();
			db_locations.onColse();
	
		}
		//优先排序已下车，待支付
		ArrayList<OfficialOrderList> list_sort = new ArrayList<OfficialOrderList>();
		if(!notOffCar){
		for(int i=0;i<list_ing.size();i++){
			if(Integer.parseInt(list_ing.get(i).getOrder_status())==Value.ORDER_STATUS_OFFCAR){
				list_sort.add(list_ing.get(i));
			}
		}
		}
		for(int i=0;i<list_ing.size();i++){
			if(Integer.parseInt(list_ing.get(i).getOrder_status())!=Value.ORDER_STATUS_OFFCAR){
				list_sort.add(list_ing.get(i));
			}
		}
		return list_sort;
		}catch(Exception e){
			return  new ArrayList<OfficialOrderList>();
		}
		
	}
	public OfficialOrderList onReadDB_ing(Context context, String order_id) {
		if(order_id==null)return null;
		MyOrderLocationsDB db_locations = new MyOrderLocationsDB(context);
		OfficialOrderList mOfficialOrderList = new OfficialOrderList();
		Cursor c = onQuery(order_id);
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				mOfficialOrderList.setId(map
						.get(DBcolumns.MYORDERING_ID));
				mOfficialOrderList.setOrder_id(map.get(DBcolumns.MYORDERING_ORDER_ID));
				mOfficialOrderList.setOrder_type(map
						.get(DBcolumns.MYORDERING_ORDER_TYPE));
				//该订单是预约时间，不是下单时间
				mOfficialOrderList.setAppoint_time(map
						.get(DBcolumns.MYORDERING_ORDER_TIME));
				//下单时间
				mOfficialOrderList.setOrder_time(map
						.get(DBcolumns.MYORDERING_XIADAN_TIME));
				mOfficialOrderList.setLine_id(map.get(DBcolumns.MYORDERING_LINE_ID));
				mOfficialOrderList.setTravel_type(map
						.get(DBcolumns.MYORDERING_TRAVEL_TYPE));
				mOfficialOrderList.setCoupon_id(map.get(DBcolumns.MYORDERING_COUPON_ID));
	
				mOfficialOrderList.setOrder_status(map
						.get(DBcolumns.MYORDERING_ORDER_STATUS));
				mOfficialOrderList.setAudit_status(map
						.get(DBcolumns.MYORDERING_AUDIT_STATUS));
				mOfficialOrderList.setValuate_status(map
						.get(DBcolumns.MYORDERING_VALUATE_STATUS));
				mOfficialOrderList.setDriver_id(map.get(DBcolumns.MYORDERING_DRIVER_ID));
				mOfficialOrderList.setDriver_name(map
						.get(DBcolumns.MYORDERING_DRIVER_NAME));
				mOfficialOrderList.setCar_NO(map.get(DBcolumns.MYORDERING_CAR_NO));
				mOfficialOrderList.setDriver_phone(map
						.get(DBcolumns.MYORDERING_DRIVER_PHONE));
				mOfficialOrderList.setDriver_star(Double.parseDouble(map
						.get(DBcolumns.MYORDERING_DRIVER_STAR)));
				mOfficialOrderList.setMessage(map
						.get(DBcolumns.MYORDERING_DRIVER_MESSAGE));
				mOfficialOrderList.setPrice(map
						.get(DBcolumns.MYORDERING_PRICE));
				mOfficialOrderList.setActual_price(map
						.get(DBcolumns.MYORDERING_ACTUALPRICE));
				mOfficialOrderList.setCoupon_sum(map
						.get(DBcolumns.MYORDERING_COUPONSUM));
				mOfficialOrderList.setTcId(map
						.get(DBcolumns.MYORDERING_TC_ID));
				mOfficialOrderList.setVehicle_id(map
						.get(DBcolumns.MYORDERING_VEHICLE_ID));
				mOfficialOrderList.setRound_trip(map
						.get(DBcolumns.MYORDERING_ROUND_TRIP));
				mOfficialOrderList.setOwn_expense(map
						.get(DBcolumns.MYORDERING_OWN));
				mOfficialOrderList.setAppoint_type(map
						.get(DBcolumns.MYORDERING_APPOINT_TYPE));
				String pay_status=map
						.get(DBcolumns.MYORDERING_PAY_STATUS);
				mOfficialOrderList.setPay_status(pay_status!=null?pay_status:Value.PAY_STATUS_NO);
				
				VehicleDetail mVehicleDetail=new VehicleDetail();
				VehicleInfo mVehicleInfo=new VehicleInfo();
				try{
				mVehicleDetail.setPurpose(map.get(DBcolumns.MYORDERING_PURPOSE));
				mVehicleDetail.setReject_reason(map.get(DBcolumns.MYORDERING_REJECT_REASON));
				mVehicleDetail.setVtype(Integer.parseInt(map.get(DBcolumns.MYORDERING_VTYPE)));
				mVehicleInfo.setVtype(Integer.parseInt(map.get(DBcolumns.MYORDERING_PAIDAN_VTYPE)));
				mVehicleDetail.setVehicleInfo(mVehicleInfo);
				}catch(Exception e){}
				mOfficialOrderList.setVehicleDetail(mVehicleDetail);
			} while (c.moveToNext());
		}else{
			return null;
		}
		c.close();
		onColse();
		
		OfficialOrderDetail mOfficialOrderDetail = new OfficialOrderDetail();
		Cursor c2 = db_locations.onQuery(order_id);
		c2.moveToFirst();
		if (c2.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c2.getColumnCount(); i++) {
					map.put(c2.getColumnName(i), c2.getString(i));
				}		
				Start start = new Start();
				start.setAddress(map
						.get(DBcolumns.MYORDER_LOCATIONS_START_ADDRESS));
				start.setName(map.get(DBcolumns.MYORDER_LOCATIONS_START_NAME));
				start.setPhone(map.get(DBcolumns.MYORDER_LOCATIONS_START_PHONE));
				Location mLocation_start = new Location();
				mLocation_start.setLat(Double.parseDouble(map
						.get(DBcolumns.MYORDER_LOCATIONS_START_LAT)));
				mLocation_start.setLng(Double.parseDouble(map
						.get(DBcolumns.MYORDER_LOCATIONS_START_LNG)));			
				start.setLocation(mLocation_start);
				End end = new End();
				end.setAddress(map.get(DBcolumns.MYORDER_LOCATIONS_END_ADDRESS));
				end.setName(map.get(DBcolumns.MYORDER_LOCATIONS_END_NAME));
				end.setPhone(map.get(DBcolumns.MYORDER_LOCATIONS_END_PHONE));
				Location mLocation_end = new Location();
				mLocation_end.setLat(Double.parseDouble(map
						.get(DBcolumns.MYORDER_LOCATIONS_END_LAT)));
				mLocation_end.setLng(Double.parseDouble(map
						.get(DBcolumns.MYORDER_LOCATIONS_END_LNG)));
				end.setLocation(mLocation_end);
				mOfficialOrderDetail.setStart(start);
				mOfficialOrderDetail.setEnd(end);
			} while (c2.moveToNext());
			c2.close();
			db_locations.onColse();
			mOfficialOrderList.setOrderDetail(mOfficialOrderDetail);
		}
		return mOfficialOrderList;
	}

	/**
	 * 写入进行中的订单数据（单笔）
	 */
	public void onWriteDB_ing(Context context, OfficialOrderList mOfficialOrderList) {
		MyOrderLocationsDB db_locations = new MyOrderLocationsDB(context);
		String myorder_id =mOfficialOrderList.getId() ;
		String myorder_order_id = mOfficialOrderList.getOrder_id();
		String myorder_order_type =mOfficialOrderList.getOrder_type();
		//该订单是预约时间，不是下单时间
		String myorder_order_time = mOfficialOrderList.getAppoint_time();
		//下单时间
		String myorder_xiadan_time = mOfficialOrderList.getOrder_time();
		String myorder_line_id = mOfficialOrderList.getLine_id();
		String myorder_travel_type =mOfficialOrderList.getTravel_type();
		String myorder_coupon_id = mOfficialOrderList.getCoupon_id();
		String myorder_order_status = mOfficialOrderList.getOrder_status();
		String myorder_audit_status = mOfficialOrderList.getAudit_status();
		String myorder_valuate_status= mOfficialOrderList.getValuate_status();	
		String myordering_driver_id=mOfficialOrderList.getDriver_id();
		String myordering_driver_name=mOfficialOrderList.getDriver_name();
		String myordering_car_No=mOfficialOrderList.getCar_NO();
		String myordering_driver_phone=mOfficialOrderList.getDriver_phone();
		String order_message=mOfficialOrderList.getMessage();
		double myordering_driver_star=mOfficialOrderList.getDriver_star();
		String price = mOfficialOrderList.getPrice();
		String totalprice = mOfficialOrderList.getTotal_price();
		String actualprice =mOfficialOrderList.getActual_price() ;
		String coupon_sum=0+"";
		if(mOfficialOrderList.getDiscount()!=null){
			coupon_sum=mOfficialOrderList.getDiscount().getSum()+"";
		}
		String tc_id=mOfficialOrderList.getTcId();
		String vehicle_type_id=mOfficialOrderList.getVehicle_id();
		String round_trip=mOfficialOrderList.getRound_trip();
		
		String purpose="";
		String reject_reason="";
		int vtype=0;
		int paidan_vtype=0;
		try{
			purpose=mOfficialOrderList.getVehicleDetail().getPurpose();
			reject_reason=mOfficialOrderList.getVehicleDetail().getReject_reason();
			vtype=mOfficialOrderList.getVehicleDetail().getVtype();
			paidan_vtype=mOfficialOrderList.getVehicleDetail().getVehicleInfo().getVtype();
			
		}catch(Exception e){}
		String own_expense=mOfficialOrderList.getOwn_expense()+"";
		String pay_status=mOfficialOrderList.getPay_status();
		String appoint_type=mOfficialOrderList.getAppoint_type();
		onWrite(myorder_id, myorder_order_id, myorder_order_type, myorder_order_time,myorder_xiadan_time,
				myorder_line_id, myorder_travel_type, myorder_coupon_id,
				myorder_order_status,myorder_audit_status,myorder_valuate_status,myordering_driver_id,myordering_driver_name,myordering_car_No
				,myordering_driver_phone,myordering_driver_star,order_message,price,totalprice,actualprice,coupon_sum,tc_id,vehicle_type_id,round_trip
				,purpose,reject_reason,vtype,paidan_vtype,own_expense, pay_status,appoint_type);
		
		//locations表
			String order_id = mOfficialOrderList.getOrder_id();
			String start_address = mOfficialOrderList.getOrderDetail()
					.getStart().getAddress();
			double start_lng = mOfficialOrderList.getOrderDetail().getStart()
					.getLocation().getLng();
			double start_lat = mOfficialOrderList.getOrderDetail().getStart()
					.getLocation().getLat();
			String start_name = mOfficialOrderList.getOrderDetail()
					.getStart().getName();
			String start_phone =mOfficialOrderList.getOrderDetail()
					.getStart().getPhone();
			String end_address = mOfficialOrderList.getOrderDetail().getEnd()
					.getAddress();
			double end_lng =mOfficialOrderList.getOrderDetail().getEnd()
					.getLocation().getLng();
			double end_lat =mOfficialOrderList.getOrderDetail().getEnd()
					.getLocation().getLat();
			String end_name = mOfficialOrderList.getOrderDetail().getEnd()
					.getName();
			String end_phone = mOfficialOrderList.getOrderDetail().getEnd()
					.getPhone();
			int volume = 0;
			int weight = 0;
			int count= 0;
			int mutiple_oncar= 0;
			db_locations.onWrite(order_id, start_address, start_lng, start_lat,
					start_name, start_phone, end_address, end_lng, end_lat,
					end_name, end_phone,volume,weight,count,mutiple_oncar);
		
	}

	/**
	 * 写入进行中的订单数据（多笔）必须是所有的数据
	 */
	public void onWriteDB_ing(final Context context, final ArrayList<OfficialOrderList> list,final boolean isDelete ,final Handler mHandler) {
		new AsyncTask<Void, Void, Void>(){
			@Override
			protected Void doInBackground(Void... params) {
				isRead=false;
				MyOrderLocationsDB db_locations = new MyOrderLocationsDB(context);
				if(isDelete){
				    onDelete();
				}
				try{
				if(list!=null&&!list.isEmpty()){
				for (int i = list.size() - 1; i >= 0; i--) {// 逆序插入，因为list最新数据在在第一个，数据库的ID要最大，所以要逆序
					String myorder_id = list.get(i).getId();
					String myorder_order_id = list.get(i).getOrder_id();
					String myorder_order_type = list.get(i).getOrder_type();
					//该订单是预约时间，不是下单时间
					String myorder_order_time = list.get(i).getAppoint_time();
					//下单时间
					String myorder_xiadan_time = list.get(i).getOrder_time();
					String myorder_line_id = list.get(i).getLine_id();
					String myorder_travel_type = list.get(i).getTravel_type();
					String myorder_coupon_id = list.get(i).getCoupon_id();
					String myorder_order_status = list.get(i).getOrder_status();
					String myorder_audit_status = list.get(i).getAudit_status();
					String myorder_valuate_status= list.get(i).getValuate_status();
					String myordering_driver_id=list.get(i).getDriver_id();
					String myordering_driver_name=list.get(i).getDriver_name();
					String myordering_car_No=list.get(i).getCar_NO();
					String myordering_driver_phone=list.get(i).getDriver_phone();
					double myordering_driver_star=list.get(i).getDriver_star();	
					String order_message=list.get(i).getMessage();
					String price = list.get(i).getPrice();
					String totalprice = list.get(i).getTotal_price();
					String actualprice = list.get(i).getActual_price();
					String coupon_sum=0+"";
					if(list.get(i).getDiscount()!=null){
						coupon_sum=list.get(i).getDiscount().getSum()+"";
					}
					String tc_id=list.get(i).getTcId();
					String vehicle_type_id=list.get(i).getVehicle_id();
					String round_trip=list.get(i).getRound_trip();
					String purpose="";
					String reject_reason="";
					int vtype=0;
					int paidan_vtype=0;
					try{
						purpose=list.get(i).getVehicleDetail().getPurpose();
						reject_reason=list.get(i).getVehicleDetail().getReject_reason();
						vtype=list.get(i).getVehicleDetail().getVtype();
						paidan_vtype=list.get(i).getVehicleDetail().getVehicleInfo().getVtype();
						
					}catch(Exception e){}
					String own_expense=list.get(i).getOwn_expense()+"";
					String pay_status=list.get(i).getPay_status();
					String appoint_type=list.get(i).getAppoint_type();
					onWrite(myorder_id, myorder_order_id, myorder_order_type,
							myorder_order_time,myorder_xiadan_time, myorder_line_id, myorder_travel_type,
							myorder_coupon_id, myorder_order_status,myorder_audit_status,myorder_valuate_status,
							myordering_driver_id,myordering_driver_name,myordering_car_No,myordering_driver_phone,myordering_driver_star,order_message,price,totalprice,actualprice,coupon_sum,tc_id,vehicle_type_id,round_trip
							,purpose,reject_reason,vtype,paidan_vtype,own_expense,pay_status,appoint_type);

		              //locations表
						String order_id = list.get(i).getOrder_id();
						String start_address = list.get(i).getOrderDetail()
								.getStart().getAddress();
						double start_lng = list.get(i).getOrderDetail().getStart()
								.getLocation().getLng();
						double start_lat = list.get(i).getOrderDetail().getStart()
								.getLocation().getLat();
						String start_name = list.get(i).getOrderDetail()
								.getStart().getName();
						String start_phone = list.get(i).getOrderDetail()
								.getStart().getPhone();
						String end_address = list.get(i).getOrderDetail().getEnd()
								.getAddress();
						double end_lng = list.get(i).getOrderDetail().getEnd()
								.getLocation().getLng();
						double end_lat = list.get(i).getOrderDetail().getEnd()
								.getLocation().getLat();
						String end_name = list.get(i).getOrderDetail().getEnd()
								.getName();
						String end_phone = list.get(i).getOrderDetail().getEnd()
								.getPhone();
						int volume = 0;
						int weight = 0;
						int count=0;
						int mutiple_oncar= 0;
						db_locations.onWrite(order_id, start_address, start_lng,
								start_lat, start_name, start_phone, end_address,
								end_lng, end_lat, end_name, end_phone,volume,weight,count,mutiple_oncar);
					
				 }
				}
				}catch(Exception e){}
				isRead=true;
				if(mHandler!=null)
				  mHandler.sendEmptyMessage(BaseBBXActivity.MSG_ORDERING_WRITE);
				return null;
			}}.execute();
	}
}
